<?php

/**
 * Created by PhpStorm.
 * User: peter.wang
 * Date: 10/16/2017
 * Time: 2:47 PM
 */
require __DIR__ . '/vendor/autoload.php';
require 'com/EncryptAES.PHP';
use GuzzleHttp\Client;
use com\EncryptAES;


class Driver
{
    //付款测试
    public function payment()
    {
        
//            $process_url = 'http://207.236.212.73:8081/process';
            $process_url = 'https://frontapi.ottpay.com/process';
            $sign_key = '1F3C702C6CF3FE2B';
            

            
            $data['amount'] = '1';
            $data['biz_type'] = 'WECHATPAY';
            $data['order_id'] = '20171007001';
            $data['call_back_url'] = 'http://www.baidu.com/success.html';
//            $data['merchant_id']='BC00002980';
            

            $md5_str = $this->generateMD5($data);
            $private_aes_key = $this->generateAESKey($md5_str,$sign_key);

            $json_str = json_encode($data);

            //DO AES & ENCODE
            $aes = new EncryptAES();
            $aes->set_key($private_aes_key);
            $aes->require_pkcs5();
            $encrypted_data = $aes->encrypt($json_str);
            $encoded_data = base64_encode(utf8_encode($encrypted_data));
            echo "md5:".$md5_str."<br/>";
            echo "json_str:".$json_str."<br/>";
            echo "key:".$private_aes_key."<br/>";
            echo "data:".$encoded_data."<br/>";
            $client = new \GuzzleHttp\Client([
                // Base URI is used with relative requests
                'base_uri' => $process_url,
                // You can set any number of default request options.
                'timeout'  => 5.0,
                'verify' => false,
                'headers' => ['Content-Type' => 'application/json']
            ]);
            $response = $client->request('POST', $process_url, [ 'json' => [
                'action' => 'ACTIVEPAY',
                'version' => '1.0',
                'merchant_id'=>'BC00002980',
                'data' => $encoded_data,
                'md5' => $md5_str
            ]]);
            $rs = $response->getBody()->getContents();
            var_dump($rs);die();
            $result = json_decode($rs,true);
            $decode_result = base64_decode($result['data']);

            $decode_aes_key = $this->generateAESKey($result['md5'],$sign_key);


            $aes->set_key($decode_aes_key);
            $aes->require_pkcs5();
            $decrypted_data = $aes->decrypt($result['data']);

      
    }

    public function generateMD5($param){
        $string = $param['amount'].$param['biz_type'].$param['call_back_url'].$param['order_id'];
        $string = md5($string);
        $string = strtoupper($string);
        return $string;
    }

    public function generateAESKey($md5_str="", $sign_key=""){
        $encode_str = $md5_str.$sign_key;
        $aes_key = substr(md5($encode_str),8,16);
        $aes_key = strtoupper($aes_key);
        return $aes_key;
    }

}

//$d = new Driver();
//$d->payment();
$private_aes_key = "DC80D181B8E08405";
$json_str = '{"amount":"1","biz_type":"WECHATPAY","order_id":"20171007001","call_back_url":"http:\/\/www.baidu.com\/success.html"}';
$aes = new EncryptAES();
$aes->set_key($private_aes_key);
$aes->require_pkcs5();
$encrypted_data = $aes->encrypt($json_str);
$encoded_data = base64_encode(utf8_encode($encrypted_data));
echo "key:".$private_aes_key."<br/>";
echo "json_data:".$json_str."<br/>";
echo "encoded_data:".$encoded_data."<br/>";
?>